package com.study.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description: 生产者与消费者
 * @Param:
 * @return:
 * @Author: 朝花不迟暮
 * @Date: 2020/5/31
 */
public class ProductAndConsumer
{
    public static void main(String[] args)
    {
        data data = new data();
//        Data2 data = new Data2();
        new Thread(() -> {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    data.add();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    data.decr();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    data.add();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }, "C").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    data.decr();
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }, "D").start();
    }
}

class data
{
    private int number = 0;

    public synchronized void add() throws InterruptedException
    {
        while (number != 0)
        {
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //去通知其他线程,自己已经执行完了
        this.notifyAll();
    }

    public synchronized void decr() throws InterruptedException
    {
        while (number == 0)
        {
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName() + "=>" + number);
        //去通知其他线程,自己已经执行完了
        this.notifyAll();
    }
}

class Data2
{
    private int number = 0;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void add() throws InterruptedException
    {
        lock.lock();
        try
        {
            while (number != 0)
            {
                condition.await();
            }
            number++;
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            condition.signalAll();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        } finally
        {
            lock.unlock();
        }

    }
    public void decr() throws InterruptedException
    {
        lock.lock();
        try
        {
            while (number == 0)
            {
                condition.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName() + "=>" + number);
            condition.signalAll();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        } finally
        {
            lock.unlock();
        }

    }
}
